/*
 * File:   Draw_time.cpp
 * Author: faris alotaibi
 *
 * Created on April 5, 2011, 2:29 PM
 */

#include "Draw_time.h"
/*
 *
 */
DrawTime::~DrawTime()
{
    this->x0 = this->y0 = this->width = this->hight = this->hour = this->minu = this->sec = 0;
}

DrawTime::DrawTime()
{
    this->x0 = 0;
    this->y0 = 0;
    this->width = 100;
    this->hight = 100;
    this->hour = -1;
    this->minu = -1;
    this->sec = -1;
}

void DrawTime::Time_loop()
{
    while(1)
    {
        this->Draw_Time(formatDefault);glFlush ( );
        //sleep(1);
    }
}

void DrawTime::Colon(int number)
{
    int colon1Top = this->y0 + ((this->hight/16)*13);
    int colon1Bottom = this->y0 + ((this->hight/16)*10);
    int colon2Top = this->y0 + ((this->hight/16)*5);
    int colon2Bottom = this->y0 + ((this->hight/16)*2);
    int left1 = this->VColon[0] + this->vx + (this->vx/2)   ,
        left2 = this->VColon[2] + this->vx + (this->vx/2) ,
        right1 = this->VColon[1] - this->vx  + (this->vx/2),
        right2 = this->VColon[3] - this->vx  + (this->vx/2) ;
    if(number == 1)
    {
        this->squareA(left1,colon1Top,right1,colon1Bottom);
        this->squareA(left1,colon2Top,right1,colon2Bottom);
    }else
    {
        this->squareA(left2,colon1Top,right2,colon1Bottom);
        this->squareA(left2,colon2Top,right2,colon2Bottom);
    }

}

void DrawTime::Clear()
{
    glColor3f(this->background[0],this->background[1],this->background[2]);
    this->squareA(this->x0,this->y0+this->hight+1,this->x0+this->width,this->y0-1);
}
void DrawTime::Color()
{
    glColor3f(this->color[0],this->color[1],this->color[2]);
}

void DrawTime::timeColor(GLfloat red,GLfloat green,GLfloat blue)
{
    this->color[0] = red;
    this->color[1] = green;
    this->color[2] = blue;
}
void DrawTime::timeBackground(GLfloat red,GLfloat green,GLfloat blue)
{
    this->background[0] = red;
    this->background[1] = green;
    this->background[2] = blue;
}

void DrawTime::Compute(Format format)
{
    if(format == FORMAT_HOUR_MIN_SEC_24)
    {
        int x = this->vx = this->x = ( this->width)/9;
        this->VHour[0] = this->x0;
        this->VHour[1] = this->x0 + 2 * x;

        this->VColon[0] = this->VHour[1];
        this->VColon[1] = this->VHour[1] + x+(x/4);

        this->VMinu[0] = this->VColon[1];
        this->VMinu[1] = this->VColon[1] + 2 * x;

        this->VColon[2] = this->VMinu[1];
        this->VColon[3] = this->VMinu[1] + x+(x/4);

        this->VSec[0] = this->VColon[3];
        this->VSec[1] = this->VColon[3] + 2 * x;

    }else if(format == FORMAT_HOUR_MIN_24)
    {
        int x = this->vx = this->x = (this->width)/6;
        this->VHour[0] = this->x0;
        this->VHour[1] = this->x0 + 2 * x;

        this->VColon[0] = this->VHour[1];
        this->VColon[1] = this->VHour[1] + x+(x/2);

        this->VMinu[0] = this->VColon[1];
        this->VMinu[1] = this->VColon[1] + 2 * x;
    }else if(format == FORMAT_HOUR_MIN_12)
    {
        int x = this->vx = this->x = ( this->width)/8;
        this->VHour[0] = this->x0;
        this->VHour[1] = this->x0 + 2 * x;

        this->VColon[0] = this->VHour[1];
        this->VColon[1] = this->VHour[1] + x+(x/2);

        this->VMinu[0] = this->VColon[1];
        this->VMinu[1] = this->VColon[1] + 2 * x;

        this->VAm[0] = this->VMinu[1];
        this->VAm[1] = this->VMinu[1] + 2 * x;
    }else if(format == FORMAT_HOUR_MIN_SEC_12)
    {
        int x = this->vx = this->x  = (this->width)/11;
        this->VHour[0] = this->x0;
        this->VHour[1] = this->x0 + 2 * x;

        this->VColon[0] = this->VHour[1];
        this->VColon[1] = this->VHour[1] + x+(x/4);

        this->VMinu[0] = this->VColon[1];
        this->VMinu[1] = this->VColon[1] + 2 * x;

        this->VColon[2] = this->VMinu[1];
        this->VColon[3] = this->VMinu[1] + x+(x/4);

        this->VSec[0] = this->VColon[3];
        this->VSec[1] = this->VColon[3] + 2 * x;

        this->VAm[0] = this->VSec[1];
        this->VAm[1] = this->VSec[1] +  2 * x;
    }
    this->vx = (((this->vx/2)-2) == 0)?((this->vx/2)-2):((this->vx/2)-1);
}

void DrawTime::initPostion(GLfloat x0, GLfloat y0)
{
    if(x0 != 0 || y0 != 0)
    {
        this->x0 = x0;
        this->y0 = y0;
    }
}

void DrawTime::initSize(GLfloat width, GLfloat hight)
{
    if(width != 100 || hight != 100)
    {
        this->width = width;
        this->hight = hight;
    }
}

void DrawTime::Draw_Time(Format format)
{
    this->formatDefault = format;
    this->Compute(format);
    time_t rawtime;
    /*for(int i=0; i<=100;i++)
    {*/
        time(&rawtime);
        this->ptime=gmtime(&rawtime);
        this->Clear();
        this->Color();
        this->Colon(1);
        if(format == FORMAT_HOUR_MIN_SEC_12 ||format == FORMAT_HOUR_MIN_SEC_24)
        {
            this->Seconds();
            this->Colon(2);
        }
        this->Minutes();
        this->Hours();
        if(format == FORMAT_HOUR_MIN_12 || format == FORMAT_HOUR_MIN_SEC_12)
            this->TypeTime();
        sleep(1);
        /*if(i=100)
            i=0;
    }*/
}
void DrawTime::TypeTime()
{
    int left1 = this->VAm[0]+5,right1 = (this->VAm[0]+this->x)-1,left2 = (this->VAm[1]-this->x)+5,right2 = this->VAm[1]-1;
    if((this->ptime->tm_hour+3) >= 13)
    {
        this->drawP(left1,this->y0+this->hight,right1,this->y0);
        this->drawM(left2,this->y0+this->hight,right2,this->y0);
    }else
    {
        this->drawA(left1,this->y0+this->hight,right1,this->y0);
        this->drawM(left2,this->y0+this->hight,right2,this->y0);
    }
}

void DrawTime::Hours()
{
    int left1 = this->VHour[0]+5,right1 = (this->VHour[0]+this->x)-1,left2 = (this->VHour[1]-this->x)+5,right2 = this->VHour[1]-1;
    if(this->hour != this->ptime->tm_hour)
    {
        this->hour=(this->formatDefault == FORMAT_HOUR_MIN_24 || this->formatDefault == FORMAT_HOUR_MIN_SEC_24)? (this->ptime->tm_hour+3)%24:1+(this->ptime->tm_hour+3)%13;
        this->selectNumber(this->hour/10,left1,this->y0+this->hight,right1,this->y0);
        this->selectNumber(this->hour%10,left2,this->y0+this->hight,right2,this->y0);
    }
}

void DrawTime::Minutes()
{
    int left1 = this->VMinu[0]+5,right1 = (this->VMinu[0]+this->x)-1,left2 = (this->VMinu[1]-this->x)+5,right2 = this->VMinu[1]-1;
    this->minu=this->ptime->tm_min;
    this->selectNumber(this->minu/10,left1,this->y0+this->hight,right1,this->y0);
    this->selectNumber(this->minu%10,left2,this->y0+this->hight,right2,this->y0);
}

void DrawTime::Seconds()
{
    int left1 = this->VSec[0]+5,right1 = (this->VSec[0]+this->x)-1,left2 = (this->VSec[1]-this->x)+5,right2 = this->VSec[1]-1;
    this->sec=this->ptime->tm_sec;
    this->selectNumber(this->sec/10,left1,this->y0+this->hight,right1,this->y0);
    this->selectNumber(this->sec%10,left2,this->y0+this->hight,right2,this->y0);
}

void DrawTime::selectNumber(int select,int left,int top, int right,int bottom)
{
    switch (select)
    {
        case 0:
            this->draw0(left,top,right,bottom);
            break;

        case 1:
            this->draw1(left,top,right,bottom);
            break;

       case 2:
            this->draw2(left,top,right,bottom);
            break;

       case 3:
            this->draw3(left,top,right,bottom);
            break;

       case 4:
            this->draw4(left,top,right,bottom);
            break;

       case 5:
            this->draw5(left,top,right,bottom);
            break;

      case 6:
            this->draw6(left,top,right,bottom);
            break;

      case 7:
            this->draw7(left,top,right,bottom);
            break;

      case 8:
            this->draw8(left,top,right,bottom);
            break;

      case 9:
            this->draw9(left,top,right,bottom);
            break;

    }
}

void DrawTime::square(int left, int top, int right ,int bottom,char side,char type)
{
    int med,square;

    if(side=='x')
    {
        square = ((right - left)/4)+2;
        if(type == 't')
        {
            glBegin(GL_POLYGON);
                glVertex2i(left+1,top);
                glVertex2i(right-1,top);
                glVertex2i(right-square,bottom);
                glVertex2i(left+square,bottom);
            glEnd();
            glFlush();
        }else if(type == 'm')
        {
            med=bottom+((top-bottom)/2);
            glBegin(GL_POLYGON);
                glVertex2i(left+1,med);
                glVertex2i(left+square,top);
                glVertex2i(right-square,top);
                glVertex2i(right-1,med);
                glVertex2i(right-square,bottom);
                glVertex2i(left+square,bottom);
            glEnd();
            glFlush();
        }else if(type == 'b')
        {
            glBegin(GL_POLYGON);
                glVertex2i(left+square,top);
                glVertex2i(right-square,top);
                glVertex2i(right-1,bottom);
                glVertex2i(left+1,bottom);
            glEnd();
            glFlush();
        }
    }else
    {
        square = (top - bottom)/9;
        if(type == 'l')
        {
            glBegin(GL_POLYGON);
                glVertex2i(left,top);
                glVertex2i(left,bottom);
                glVertex2i(right,bottom+square);
                glVertex2i(right,top-square);
            glEnd();
            glFlush();
        }else if(type == 'm')
        {
            med=left+((right-left)/2);
            glBegin(GL_POLYGON);
                glVertex2i(med,top-1);
                glVertex2i(left,top-square);
                glVertex2i(left,bottom+square);
                glVertex2i(med,bottom+1);
                glVertex2i(right,bottom+square);
                glVertex2i(right,top-square);
            glEnd();
            glFlush();
        }else if(type == 'r')
        {
            glBegin(GL_POLYGON);
                glVertex2i(left,top-square);
                glVertex2i(left,bottom+square);
                glVertex2i(right,bottom);
                glVertex2i(right,top);
            glEnd();
            glFlush();
        }
    }
}

void DrawTime::squareA(int left, int top, int right ,int bottom)
{
        glBegin(GL_POLYGON);
            glVertex2i(left,top);
            glVertex2i(right,top);
            glVertex2i(right,bottom);
            glVertex2i(left,bottom);
        glEnd();
        glFlush();

}

void DrawTime::draw0(int left,int top,int right,int bottom)
{
    int squareY = ((top - bottom)/9)/2;
    int squareX = (right - left)/4;
    this->square(left,top,right,top-squareY,'x','t');
    this->square(left,top,left+squareX,(bottom+(top-bottom)/2),'y','l');
    this->square(right-squareX,top,right,(bottom+(top-bottom)/2),'y','r');
    this->square(left,(bottom+(top-bottom)/2),left+squareX,bottom,'y','l');
    this->square(right-squareX,(bottom+(top-bottom)/2),right,bottom,'y','r');
    this->square(left,bottom+squareY,right,bottom,'x','b');
}

void DrawTime::draw1(int left,int top,int right,int bottom)
{
    int squareX = (right - left)/4;
    int squareY = ((top - bottom)/9)/2;
    this->square(right-squareX,top,right,(bottom+(top-bottom)/2),'y','m');
    this->square(right-squareX,(bottom+(top-bottom)/2),right,bottom,'y','m');
}

void DrawTime::draw2(int left,int top,int right,int bottom)
{
    int squareY = ((top - bottom)/9)/2;
    int squareX = (right - left)/4;
    this->square(left,top,right,top-squareY,'x','t');
    this->square(right-squareX,top,right,(bottom+(top-bottom)/2),'y','r');
    this->square(left,(bottom+(top-bottom)/2)-(squareY/2),right,(bottom+(top-bottom)/2)+(squareY/2),'x','m');
    this->square(left,(bottom+(top-bottom)/2),left+squareX,bottom,'y','l');
    this->square(left,bottom+squareY,right,bottom,'x','b');
}

void DrawTime::draw3(int left,int top,int right,int bottom)
{
    int squareY = ((top - bottom)/9)/2;
    int squareX = (right - left)/4;
    this->square(left,top,right,top-squareY,'x','t');
    this->square(right-squareX,top,right,bottom+(top-bottom)/2,'y','r');
    this->square(left,(bottom+(top-bottom)/2)-(squareY/2),right,(bottom+(top-bottom)/2)+(squareY/2),'x','m');
    this->square(right-squareX,(bottom+(top-bottom)/2),right,bottom,'y','r');
    this->square(left,bottom+squareY,right,bottom,'x','b');
}

void DrawTime::draw4(int left,int top,int right,int bottom)
{
    int squareY = ((top - bottom)/9)/2;
    int squareX = (right - left)/4;
    this->square(left,top,left+squareX,bottom+(top-bottom)/2,'y','l');
    this->square(right-squareX,top,right,bottom+(top-bottom)/2,'y','r');
    this->square(left,(bottom+(top-bottom)/2)-(squareY/2),right,(bottom+(top-bottom)/2)+(squareY/2),'x','m');
    this->square(right-squareX,(bottom+(top-bottom)/2),right,bottom,'y','r');
}

void DrawTime::draw5(int left,int top,int right,int bottom)
{
    int squareY = ((top - bottom)/9)/2;
    int squareX = (right - left)/4;
    this->square(left,top,right,top-squareY,'x','t');
    this->square(left,top,left+squareX,bottom+(top-bottom)/2,'y','l');
    this->square(left,(bottom+(top-bottom)/2)-(squareY/2),right,(bottom+(top-bottom)/2)+(squareY/2),'x','m');
    this->square(right-squareX,(bottom+(top-bottom)/2),right,bottom,'y','r');
    this->square(left,bottom+squareY,right,bottom,'x','b');
}

void DrawTime::draw6(int left,int top,int right,int bottom)
{
    int squareY = ((top - bottom)/9)/2;
    int squareX = (right - left)/4;
    this->square(left,top,right,top-squareY,'x','t');
    this->square(left,top,left+squareX,bottom+(top-bottom)/2,'y','l');
    this->square(left,(bottom+(top-bottom)/2)-(squareY/2),right,(bottom+(top-bottom)/2)+(squareY/2),'x','m');
    this->square(left,(bottom+(top-bottom)/2),left+squareX,bottom,'y','l');
    this->square(right-squareX,(bottom+(top-bottom)/2),right,bottom,'y','r');
    this->square(left,bottom+squareY,right,bottom,'x','b');
}

void DrawTime::draw7(int left,int top,int right,int bottom)
{
    int squareY = ((top - bottom)/9)/2;
    int squareX = (right - left)/4;
    this->square(left,top,right,top-squareY,'x','t');
    this->square(right-squareX,top,right,bottom+(top-bottom)/2,'y','r');
    this->square(right-squareX,(bottom+(top-bottom)/2),right,bottom,'y','r');
}

void DrawTime::draw8(int left,int top,int right,int bottom)
{
    int squareY = ((top - bottom)/9)/2;
    int squareX = (right - left)/4;
    this->square(left,top,right,top-squareY,'x','t');
    this->square(left,top,left+squareX,bottom+(top-bottom)/2,'y','l');
    this->square(right-squareX,top,right,bottom+(top-bottom)/2,'y','r');
    this->square(left,(bottom+(top-bottom)/2)-(squareY/2),right,(bottom+(top-bottom)/2)+(squareY/2),'x','m');
    this->square(left,(bottom+(top-bottom)/2),left+squareX,bottom,'y','l');
    this->square(right-squareX,(bottom+(top-bottom)/2),right,bottom,'y','r');
    this->square(left,bottom+squareY,right,bottom,'x','b');
}

void DrawTime::draw9(int left,int top,int right,int bottom)
{
    int squareY = ((top - bottom)/9)/2;
    int squareX = (right - left)/4;
    this->square(left,top,right,top-squareY,'x','t');
    this->square(left,top,left+squareX,bottom+(top-bottom)/2,'y','l');
    this->square(right-squareX,top,right,bottom+(top-bottom)/2,'y','r');
    this->square(left,(bottom+(top-bottom)/2)-(squareY/2),right,(bottom+(top-bottom)/2)+(squareY/2),'x','m');
    this->square(right-squareX,(bottom+(top-bottom)/2),right,bottom,'y','r');
    this->square(left,bottom+squareY,right,bottom,'x','b');
}

void DrawTime::drawA(int left,int top,int right,int bottom)
{
    int squareY = ((top - bottom)/9)/2;
    int squareX = (right - left)/6;
    this->square(left,top,right,top-squareY,'x','t');
    this->square(left,top,left+squareX,bottom+(top-bottom)/2,'y','l');
    this->square(right-squareX,top,right,bottom+(top-bottom)/2,'y','r');
    this->square(left,(bottom+(top-bottom)/2)-(squareY/2),right,(bottom+(top-bottom)/2)+(squareY/2),'x','m');
    this->square(left,(bottom+(top-bottom)/2),left+squareX,bottom,'y','l');
    this->square(right-squareX,(bottom+(top-bottom)/2),right,bottom,'y','r');
    //this->square(left,bottom+squareY,right,bottom,'x','b');
}
void DrawTime::drawP(int left,int top,int right,int bottom)
{
    int squareY = ((top - bottom)/9)/2;
    int squareX = (right - left)/6;
    this->square(left,top,right,top-squareY,'x','t');
    this->square(left,top,left+squareX,bottom+(top-bottom)/2,'y','l');
    this->square(right-squareX,top,right,bottom+(top-bottom)/2,'y','r');
    this->square(left,(bottom+(top-bottom)/2)-(squareY/2),right,(bottom+(top-bottom)/2)+(squareY/2),'x','m');
    this->square(left,(bottom+(top-bottom)/2),left+squareX,bottom,'y','l');
    //this->square(right-squareX,(bottom+(top-bottom)/2),right,bottom,'y','r');
    //this->square(left,bottom+squareY,right,bottom,'x','b');
}
void DrawTime::drawM(int left,int top,int right,int bottom)
{
    int squareY = ((top - bottom)/9)/2;
    int squareX = (right - left)/6;
    this->square(left,top,left+(squareX*4),top-squareY,'x','t');
    this->square(right-(squareX*4),top,right,top-squareY,'x','t');
    this->square(left+((squareX*4)-squareX/2),top,right-((squareX*4)-squareX/2),bottom+(top-bottom)/2,'y','m');
    this->square(left+((squareX*4)-squareX/2),bottom+(top-bottom)/2,right-((squareX*4)-squareX/2),bottom,'y','m');
    this->square(left,top,left+squareX,bottom+(top-bottom)/2,'y','l');
    this->square(right-squareX,top,right,bottom+(top-bottom)/2,'y','r');
    this->square(left,(bottom+(top-bottom)/2),left+squareX,bottom,'y','l');
    this->square(right-squareX,(bottom+(top-bottom)/2),right,bottom,'y','r');
    //this->square(left,bottom+squareY,right,bottom,'x','b');
}
